/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/* 
 * Copyright (c) 2009 City University of Hong Kong
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License m_version 2 as
 * published by the Free Software Foundation;
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 * 
 * Author: Wilson Thong (wilsonwk@ee.cityu.edu.hk) 
 */
#ifndef DEFLECTION_ROUTING_MANAGER_H
#define DEFLECTION_ROUTING_MANAGER_H

#include "ipv4-deflection-routing.h"
#include "base-topology.h"
#include "ns3/callback.h"
#include "ns3/traced-callback.h"
#include "ns3/random-variable.h"
#include "ns3/ipv4-routing-helper.h"
#include "ns3/log.h"
#include "ns3/ipv4-global-routing-helper.h"
#include "ns3/ipv4-static-routing-helper.h"

namespace ns3 {
namespace eecityu {

/**
 * \brief Install a deflection routing protocol to all nodes of a topology
 */
class Ipv4DeflectionRoutingHelper : public Ipv4RoutingHelper
{
public:
  Ipv4DeflectionRoutingHelper ();
  /**
   * \brief Calculates routing tables on all nodes
   *
   * Ipv4DeflectionRouting relies on the routes database from Ipv4GlobalRouting.
   * Before Ipv4DeflectionRouting can function, these routing tables must be
   * available on nodes. 
   *
   * The method simply calls the Ipv4GlobalRouting::PopulateRoutingTables ()
   */
  static void PopulateRoutingTables ();
  /**
   * \deprecated
   *
   * \brief Enable the nodes to run the deflection routing protocol.
   *
   * (Implemenation code are copied and modified from 
   * GlobalRouteManagerImpl::SelectRouterNodes (NodeContainer c) )
   *
   * \param net A topology on which deflection routing is enabled
   */
  static uint32_t PopulateRoutingTables (const BaseTopology& net) NS_DEPRECATED;
  /**
   * \deprecated
   *
   * \brief Enable the nodes to run the deflection routing protocol.
   *
   * (Implemenation code are copied and modified from 
   * GlobalRouteManagerImpl::SelectRouterNodes (NodeContainer c) )
   *
   * \param net A topology on which deflection routing is enabled
   * \param selectPb The probability of a node being selected to install deflection routing protocol.
   * The value must be in the range from 0.0 to 1.0 (both inclusive)
   */
  static uint32_t PopulateRoutingTables (const BaseTopology& net, double selectedPb) NS_DEPRECATED;
  /**
   * \deprecated
   *
   * \brief Enable the selected nodes to run the deflection routing protocol.
   *
   * (Implemenation code are copied and modified from 
   * GlobalRouteManagerImpl::SelectRouterNodes (NodeContainer c) )
   *
   * \param net A topology on which deflection routing is enabled.
   * \param selectedNodeCallback A callback taking a Ptr<Node> as an argument and return true if the node
   * is selected to install deflection routing
   */
  static uint32_t PopulateRoutingTables (const BaseTopology& net, const Callback<bool, Ptr<Node> >& selectedNodeCallback) NS_DEPRECATED;
  /**
   * \brief Get a Ipv4ListRoutingHelper when the Ipv4DeflectionRoutingHelper 
   * added into it
   *
   * Ipv4DeflectionRouting cannot operate on its own. It needs the Ipv4GlobalRouting
   * to calculate routing tables. Thus, this method returns a Ipv4ListRoutingHelper
   * in which a Ipv4GlobalRoutingHelper and Ipv4DeflectionRoutingHelper are added
   * with appropriate priority.
   *
   * When installing deflection routing helper to a node, it must install the
   * Ipv4ListRoutingHelper returned by this method, not the 
   * Ipv4DeflectionRoutingHelper itself.
   */
  Ipv4ListRoutingHelper GetIpv4ListRoutingHelper () const;
  /**
   * \param node the node on which the routing protocol will run
   * \returns a newly-created routing protocol
   *
   * The created Ipv4DeflectionRouting object can be accessed
   * using ns3::Names service. The object can be retrieved from 
   * the path
   *   "Ipv4DeflectionRouting'x'"
   * where 'x' is the node ID on which the Ipv4DeflectionRouting
   * object is installed
   *
   * This method will be called by ns3::InternetStackHelper::Install
   */
  virtual Ptr<Ipv4RoutingProtocol> Create (Ptr<Node> node) const;
  /**
   * \internal
   * \returns pointer to clone of this Ipv4StaticRoutingHelper
   *
   * This method is mainly for internal use by the other helpers;
   * clients are expected to free the dynamic memory allocated by this method
   */
  virtual Ipv4RoutingHelper* Copy (void) const;

private:
  /**
   * \brief The implementation of the PopulateRoutingTables () static method
   */
  uint32_t PopulateRoutingTablesImpl (const BaseTopology& net);
  /**
   * \brief The implementation of the PopulateRoutingTables () static method
   */
  uint32_t PopulateRoutingTablesImpl (const BaseTopology& net, double selectedPb);
  /**
   * \brief The implementation of the PopulateRoutingTables () static method
   */
  uint32_t PopulateRoutingTablesImpl (const BaseTopology& net, const Callback<bool, Ptr<Node> >& selectedNodeCallback);
  /**
   * \brief Build in random variable to select node for deflection routing protocol
   *
   * \param r The probability of selecting the node n to install deflection routing protocol
   * \param n The node being considered installing deflection routing protocol
   */
  static bool RandomNodeSelector (double r, Ptr<Node> n);


  /// \brief Trace source triggered when a node is selected to install a deflection routing protocol
  TracedCallback<Ptr<Node> > m_nodeSelectedTrace;
  /// \brief Trace source triggered when a node is skipped for installing a deflection routing protocol
  TracedCallback<Ptr<Node> > m_nodeSkippedTrace;
  /// \brief Random selector to select nodes and do deflection routing protocol installation
  static UniformVariable g_randSelector;
};

} // namespace eecityu
} // namespace ns3

#endif // #ifndef DEFLECTION_ROUTING_MANAGER_H
